<script setup lang="ts">
import type { Schema } from '../types/comp';

type Props = {
  type: 'default' | 'form';
  list: Schema[];
};

import { ref, useTemplateRef } from 'vue';

import { Modal } from 'ant-design-vue';

import Form from 'custom/form';

const open = defineModel<boolean>({ default: false });

withDefaults(defineProps<Props>(), {
  type: 'default',
});

const emits = defineEmits(['ok']);

const refForm = useTemplateRef('refForm');

const confirmLoading = ref(false);

const model = ref({});

const handleOk = async () => {
  const res = await refForm.value?.handleSubmit();
  if (res) {
    confirmLoading.value = true;
    emits('ok', () => {
      open.value = false;
      confirmLoading.value = false;
    });
  }
};
</script>

<template>
  <Modal title="test" v-model:open="open" :confirm-loading="confirmLoading" @ok="handleOk">
    <template v-if="type === 'default'">
      <slot />
    </template>
    <template v-else-if="type === 'form'">
      <Form v-model="model" :col="24" :list="list" ref="refForm" />
    </template>
  </Modal>
</template>
